// pages/in/in.js
var app =getApp()
import {
  async
} from '../../utils/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    name: '',
    content: '',
    shiping: '',
    carID: '',
    video: [],
    modalName: '',
    fixContent: '',
    value: "",
    zhaopian: [],
    shiping: [],
    b: 0
  },
  onLoad(e){
    if(wx.getStorageSync('is') != true){
      var index  = 0
      var s = setInterval(function(e){
        // console.log(app.globalData.openid)
        if(app.globalData.openid!=''){
          for(var i = 0 ; i < app.globalData.a.length ; i++){
            // console.log(app.globalData.a[i])
            if(app.globalData.openid == app.globalData.a[i]){
              index = 1
              
            }else{
              index = 1
            }
          }
          if(index == 1){
            clearInterval(s)
            wx.setStorageSync('is', true)
            wx.redirectTo({
              url: '../index/index',
            })
          }
        }
      },500)
    }else if(e.id != 1){
      wx.redirectTo({
        url: '../index/index',
      })
    }
    
  },
  changes(e) {
    this.setData({
      value: e.detail.value
    })
  },
  change(e) {
    if (this.data.modalName == '维修人员') {
      this.setData({
        name: this.data.value,
        value: ''
      })
    } else {
      this.setData({
        carID: this.data.value,
        value: ''
      })
    }
    this.setData({
      modalName: ''
    })
  },
  onTap(e) {
    console.log(e.currentTarget.dataset.id)
    this.setData({
      modalName: e.currentTarget.dataset.id
    })
  },
  in () {
    wx.chooseMessageFile({
      count: 10,
      success: (res) => {
        console.log(res.tempFiles)

      }
    })
  },
  up(url, k) {
    var that = this
    let MyFile = new wx.BaaS.File()
    let fileParams = {}
    let metaData = {}
    var items
    if (k == 1) {
      items = that.data.shiping
      fileParams = {
        filePath: url,
        fileType: "video"
      }

      metaData = {
        categoryName: '故障视频'
      }
    } else {
      items = that.data.zhaopian
      fileParams = {
        filePath: url,
        fileType: "image"
      }

      metaData = {
        categoryName: '故障照片'
      }
    }


    MyFile.upload(fileParams, metaData).then(res => {
      // 上传成功
      let data = res.data // res.data 为 Object 类型
      console.log(data)
      items.push(res.data.path)
      if (k == 1) {
        that.setData({
          shiping: items,
          b: that.data.b + 1
        })
      } else {
        that.setData({
          zhaopian: items,
          b: that.data.b + 1
        })
      }
    }, err => {
      console.log(err)
      // HError 对象
    })

  },

  ups: function () {
    var that = this
    return new Promise((resolve, reject) => {
      for (var i = 0; i < that.data.video.length; i++) {
        that.up(that.data.video[i], 1)
      }
      for (var j = 0; j < that.data.imgList.length; j++) {
        that.up(that.data.imgList[j], 2)
      }
      var ui = setInterval(function (e) {
        console.log(that.data.b)
        if (that.data.b ==  that.data.imgList.length + that.data.video.length) {
          resolve(1)
          clearInterval(ui)
        }
      }, 1000)
    })
  },
  from: async function () {
    var that = this
    var i = 0
    wx.showToast({
      title: '上传中...',
      mask: true,
      icon: 'loading'
    })
    var a = await that.ups();
    // 向 tableName 为 'product' 的数据表插入一条记录
    if (a == 1) {
      let tableName = 'suopei'
      let Product = new wx.BaaS.TableObject(tableName)
      let product = Product.create()
      var date = new Date()
      var year = date.getFullYear();
      var mouth = date.getMonth() + 1;
      var day = date.getDate()
      // 设置方式二
      product.set('zhaopian', that.data.zhaopian)
      product.set('shiping', that.data.shiping)
      product.set('content', that.data.content)
      product.set('fixContent', that.data.fixContent)
      product.set('carID', that.data.carID)
      product.set('shijian', year + '.' + mouth + '.' + day)
      product.set('people', that.data.name)
      product.save().then(res => {
        // success
        wx.hideToast({})
        console.log(res)
        wx.showToast({
          title: '上传成功',
          icon:'success',
          duration:2000
        })
        wx.redirectTo({
          url: '../in/in?id='+1,
        })
      }, err => {
        // HError 对象
      })


    }

  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  /**
   * 选择举报图片
   */
  ChooseImage() {
    wx.chooseImage({
      count: 9, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },

  inshi() {
    var that = this
    wx.chooseMedia({
      count: 4,
      mediaType: ["video"],
      success: res => {
        console.log(res.tempFiles)
        for(var i = 0 ; i<res.tempFiles.length;i++){
          that.data.video.push(res.tempFiles[i].tempFilePath)
        }
        
        that.setData({
          video: that.data.video
        })

      }
    })
  },

  delv(e) {
    var that = this
    wx.showModal({
      content: '确定要删除？',
      cancelText: '不了',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.video.splice(e.currentTarget.dataset.id, 1);
          this.setData({
            video: this.data.video
          })
        }
      }
    })
  },
  /**
   * 删除举报图片
   * @param {}} e 
   */
  DelImg(e) {
    wx.showModal({
      content: '确定要删除？',
      cancelText: '不了',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  hideModal() {
    this.setData({
      modalName: ''
    })
  },
  setMessage(e) {
    this.setData({
      content: e.detail.value
    })
  },

  setMessage1(e) {
    this.setData({
      fixContent: e.detail.value
    })
  }
})